package com.ifp.opengate.boot.validator.service;

import org.springframework.stereotype.Component;

import com.ifp.opengate.boot.bean.ValidateEntity;
import com.ifp.opengate.boot.bean.ValidatorForm;
import com.ifp.opengate.boot.validator.constants.ValidatorConstants;

import cn.hutool.core.util.ReUtil;

@Component
public class PatternValidateServiceImpl extends ValidateServiceImpl {

    @Override
    public ValidateEntity validateValue(ValidatorForm validatorForm, Object data) {
        if (!(data instanceof String)) {
            String message = validatorForm.getErrorMsg();
            return new ValidateEntity(ValidatorConstants.DEFAULT_ERROR_CODE, message, data,
                validatorForm.getParamName());
        }
        String patternStr = validatorForm.getCheckPattern();
        boolean pass = ReUtil.isMatch(patternStr, (String)data);
        if (pass) {
            return null;
        }
        String message = validatorForm.getErrorMsg();
        return new ValidateEntity(ValidatorConstants.DEFAULT_ERROR_CODE, message, data, validatorForm.getParamName());
    }
}